package com.oa.xinxi.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oa.login.biz.EmpBiz;
import com.oa.login.entity.Emp;
import com.oa.office.biz.ReportTableBiz;
import com.oa.office.entity.ReportTable;
import com.oa.office.entity.ReportTableWithBLOBs;
import com.oa.xinxi.biz.WorkPlanBiz;
import com.oa.xinxi.entity.WorkPlan;

@Controller
@RequestMapping("/wp")
public class WorkPlanController {
	@Resource
	private WorkPlanBiz workPlanBiz;
	@Resource
	private EmpBiz empBiz;
	@Resource
	private ReportTableBiz rtBiz;
	@RequestMapping("/list")
	public String list(Model model,@RequestParam(required=true,defaultValue="1") Integer page) {
		PageHelper.startPage(page, 10);
		List<WorkPlan> list = workPlanBiz.findAll();
		PageInfo<WorkPlan> p=new PageInfo<WorkPlan>(list);
		model.addAttribute("list", list);
		model.addAttribute("page", p);
		return "/xinxi/wp/list";
	}
	@RequestMapping("/preAdd")
	public String add() {
		return "/xinxi/wp/add";
	}
	@RequestMapping("/add")
	public String save(WorkPlan workPlan,HttpSession session) {
//		System.out.println("添加");
		
		Emp emp = (Emp)session.getAttribute("emp");
		workPlan.setCreator(emp.getEmpName());
		workPlanBiz.save(workPlan);
		return "redirect:/wp/list";
	}
	@RequestMapping("/detail/{id}")
	public String detail(Model model,@PathVariable("id") int id) {
		WorkPlan workPlan = workPlanBiz.findById(id);
		model.addAttribute("workPlan", workPlan);
		return "/xinxi/wp/detail";
	}
	@RequestMapping("/delete/{id}")
	public String deleteById(Model model,@PathVariable("id") int id) {
		workPlanBiz.deleteById(id);
		return "redirect:/wp/list";
	}
	@RequestMapping("/preUpdate/{id}")
	public String updateById(Model model,@PathVariable("id") int id) {
		 WorkPlan workPlan = workPlanBiz.findById(id);
		 model.addAttribute("workPlan", workPlan);
		return "/xinxi/wp/update";
	}
	@RequestMapping("/update")
	public String DoUpdateById(WorkPlan workPlan,HttpSession session) {
//		System.out.println(news.getContent());
		Emp emp = (Emp)session.getAttribute("emp");
		workPlan.setUpdator(emp.getEmpName());
		workPlanBiz.update(workPlan);
		return "redirect:/wp/list";
	}
	@RequestMapping("/preAddFromEmp")
	public String addFromEmp(Model model) {
		List<Emp> list = empBiz.findAlls();
		model.addAttribute("list", list);
		return "/xinxi/wp/addFromEmp";
	}
	@RequestMapping("/wpList/{name}")
	public String wpList(Model model,@PathVariable("name")String name) {
		List<ReportTableWithBLOBs> list = rtBiz.findByEmpName(name);
		model.addAttribute("list", list);
		return "/xinxi/wp/wpList";
	}
}
